//
//  BouncingBall.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/16/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "BouncingBall.h"
#import "MainGameScene.h"
#import "BombBossIndicator.h"

@implementation BouncingBall
@synthesize _Vx = mVx, _Vy = mVy;
@synthesize _userNode = mUserNode;
@synthesize _bossType = mBossType;
@synthesize _timeBomb = mTimeBomb;

-(id) init
{
    if (self = [super initWithSpriteFrameName:[self getSpriteFrameName]])
    {
        mTimeBombCounting = 1000;
        mEye = [[CCSprite spriteWithSpriteFrameName:SFN_BOSS_EYE] retain];
        mGlow = [[CCSprite spriteWithSpriteFrameName:SFN_NODE_INDICATOR] retain];
        [mGlow setOpacity:0];
        [mGlow setScale:(self.contentSize.width / uniPos(16, 32) / 2)];
        [self schedule:@selector(updateEye)];
        [self createBody];
    }
    
    return self;
}

-(CCSprite *) getEye
{
    return mEye;
}

-(CCSprite *) getGlow
{
    return mGlow;
}

-(void) createBody
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0, 0);
    
    _b2Body = PhysicSystem::getInstance()->mWorld->CreateBody(&bodyDef);
    
    b2CircleShape shape;
    shape.m_radius = (self.contentSize.width / 2) / PTM_RATIO;
    
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.filter.categoryBits = [self getObjectType];
    fixtureDef.filter.maskBits = MASK_BIT_BOUNC_BALL;
    fixtureDef.isSensor = true;
    _b2Body->CreateFixture(&fixtureDef);
    
    [self setPTMRatio:PTM_RATIO];
    _b2Body->SetUserData(self);
}

-(OBJECT_TYPE) getObjectType
{
    
    return eO_BOUNC_BALL;
}

-(void) addActions:(NSArray *)actions
{

}

-(void) startAction
{
    [self scheduleUpdate];
    mTimeBombCounting = mTimeBomb;
}

-(void) stopAction
{
    [self stopAllActions];
}

-(void) update:(ccTime)delta
{
    [self setPosition:ccp(self.position.x + mVx * delta, self.position.y + mVy * delta)];
    
    float boundary = self.contentSize.width / 2;
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    
    
    float newX = self.position.x;
    float newY = self.position.y;
    if (self.position.x < boundary)
    {
        mVx *= -1;
        newX = boundary;
    }
    else if (self.position.x > winSize.width - boundary)
    {
        mVx *= -1;
        newX = winSize.width - boundary;
    }
    
    if (self.position.y < boundary + uniPos(40, 80))
    {
        mVy *= -1;
        newY = boundary + uniPos(40, 80);
    }
    else if (self.position.y > winSize.height - boundary - uniPos(40, 80))
    {
        mVy *= -1;
        newY = winSize.height - boundary - uniPos(40, 80);
    }
    
    [self setPosition:ccp(newX, newY)];
    
//    [self updateEye];
    
    // update bomb
    
    if ([MainGameScene getInstance]._mainGameState == sMG_STARTING)
    {
        mTimeBombCounting -= delta;
        if (mTimeBombCounting <= 0)
        {
            // reset time
            mTimeBombCounting = mTimeBomb;
            
            // glow boss
            CCFadeTo *fade1 = [CCFadeTo actionWithDuration:0.25 opacity:255];
            CCDelayTime *delay = [CCDelayTime actionWithDuration:0.5];
            CCFadeTo *fade2 = [CCFadeTo actionWithDuration:0.25 opacity:0];
            CCSequence *sequence = [CCSequence actions:fade1, delay, fade2, nil];
            [mGlow runAction:sequence];
            
            
            // hit bomb
            if (mBossType == eBT_ONESHOT)
            {
                [self bombOneShot];
            }
            else if (mBossType == eBT_RANDOM)
            {
                [self bombRandom];
            }
            else if (mBossType == eBT_MATRIX)
            {
                [self bombMatrix];
            }
        }
    }
}

-(void) updateEye
{
    // update eye
    CGPoint sub = ccpSub([mUserNode getRealPosition], self.position);
    float angle = atan2f(sub.y, sub.x);
    [mEye setPosition:ccp(self.position.x + cosf((angle)) * uniPos(8, 16), self.position.y + sinf(angle) * uniPos(8, 16))];
    [mGlow setPosition:self.position];
}

-(void) bombOneShot
{
    BombBossIndicator *bb = [[BombBossIndicator alloc] initWithPos:[mUserNode getRealPosition]];
    [[MainGameScene getInstance] addChild:bb];
    [bb release];
}

-(void) bombRandom
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    for (int i = 0; i < 10; i++)
    {
        int rx = rand() % (int)winSize.width;
        int ry = rand() % (int)winSize.height;
        BombBossIndicator *bb = [[BombBossIndicator alloc] initWithPos:ccp(rx, ry)];
        [[MainGameScene getInstance] addChild:bb];
        [bb release];
    }
}

-(void) bombMatrix
{
    
}

-(NSString *) getSpriteFrameName
{
    return SFN_BOSS_BODY;
}

-(void) dealloc
{
    [mEye release];
    [mGlow release];
    [super dealloc];
    CCLOG(@"BouncingBall dealloc!");
}

@end
